package edu.utd.text.game.controller.commands;

import java.util.ArrayList;

import edu.utd.text.game.controller.Controller;
import edu.utd.text.game.domain.GameObject;
import edu.utd.text.game.domain.exceptions.GameObjectNotFoundException;
import edu.utd.text.game.domain.exceptions.ItemAlreadyExistsInInventoryException;
import edu.utd.text.game.domain.exceptions.MaxItemsExceededException;
import edu.utd.text.game.domain.item.GameItem;


public class PickupCommand implements GameCommand {
	
	private String objectToPickup;
	
	public PickupCommand(String objectToPickup) {
		this.objectToPickup = objectToPickup;
	}
	
	public String getObjectToPickup() { return objectToPickup; }
	
	public void setObjectToPickup(String objectToPickup) { this.objectToPickup = objectToPickup; }

	
	
	public String execute() {
		Controller controller = Controller.INSTANCE;
		ArrayList<GameObject> objectsInRoom = controller.getPlayer().getCurrentRoom().getObjects();
		
		for (GameObject gameObject : objectsInRoom) {
			if (gameObject.tagsMatch(objectToPickup)) {
				if (gameObject instanceof GameItem) { //maybe this rule should be enforced in Player
					try {
						controller.getPlayer().pickupItem((GameItem)gameObject);
						return "Got " + gameObject.getName();
					} 
					catch (ItemAlreadyExistsInInventoryException iaeiix) {
						return "You are already carrying one of those!";
					}
					catch (MaxItemsExceededException miex) {
						return "You can't hold any more items in your inventory.  Drop something first.";
					}
					catch (GameObjectNotFoundException gonfx) {
						controller.handleException(gonfx);
					}
				}
				else {
					return "You can't carry that!";
				}
			}
		}
		return "I don't see one of those here.";
	}

	
}
